# 集成测试脚本
# 测试服务间的集成和完整工作流

param(
    [string]$BaseUrl = "http://localhost:8080"
)

$ErrorActionPreference = "Stop"

Write-Host "🧪 SmartCodeService 集成测试" -ForegroundColor Cyan
Write-Host "================================" -ForegroundColor Cyan
Write-Host ""

# 测试场景1: 完整的代码补全流程
function Test-CodeCompletionFlow {
    Write-Host "📝 测试场景1: 代码补全完整流程" -ForegroundColor Yellow
    
    # 1. 准备代码上下文
    $context = @{
        filePath = "foundation/arkui/napi/src/test.cpp"
        code = "void testFunction() {"
        cursorLine = 1
        cursorColumn = 25
        projectRoot = "C:/AI_Dev/SmartCodeService/ohcode"
    }
    
    # 2. 调用补全API
    try {
        $response = Invoke-RestMethod -Uri "$BaseUrl/api/completion/suggest" `
            -Method POST `
            -ContentType "application/json" `
            -Body ($context | ConvertTo-Json -Depth 10)
        
        if ($response.code -eq 200 -and $response.data.Count -gt 0) {
            Write-Host "  ✅ 代码补全成功，返回 $($response.data.Count) 个建议" -ForegroundColor Green
            return $true
        } else {
            Write-Host "  ❌ 代码补全失败或无结果" -ForegroundColor Red
            return $false
        }
    } catch {
        Write-Host "  ❌ 错误: $($_.Exception.Message)" -ForegroundColor Red
        return $false
    }
}

# 测试场景2: 代码导航流程
function Test-NavigationFlow {
    Write-Host "🧭 测试场景2: 代码导航流程" -ForegroundColor Yellow
    
    # 1. 跳转到定义
    $definitionRequest = @{
        symbol = "testFunction"
        filePath = "foundation/arkui/napi/src/test.cpp"
        projectRoot = "C:/AI_Dev/SmartCodeService/ohcode"
    }
    
    try {
        $defResponse = Invoke-RestMethod -Uri "$BaseUrl/api/completion/navigation/definition" `
            -Method POST `
            -ContentType "application/json" `
            -Body ($definitionRequest | ConvertTo-Json -Depth 10)
        
        if ($defResponse.code -eq 200 -and $defResponse.data) {
            Write-Host "  ✅ 跳转到定义成功" -ForegroundColor Green
            
            # 2. 查找引用
            $refResponse = Invoke-RestMethod -Uri "$BaseUrl/api/completion/navigation/references" `
                -Method POST `
                -ContentType "application/json" `
                -Body ($definitionRequest | ConvertTo-Json -Depth 10)
            
            if ($refResponse.code -eq 200) {
                Write-Host "  ✅ 查找引用成功，找到 $($refResponse.data.Count) 个引用" -ForegroundColor Green
                return $true
            }
        }
        
        Write-Host "  ❌ 代码导航失败" -ForegroundColor Red
        return $false
    } catch {
        Write-Host "  ❌ 错误: $($_.Exception.Message)" -ForegroundColor Red
        return $false
    }
}

# 测试场景3: Agent对话流程
function Test-AgentFlow {
    Write-Host "🤖 测试场景3: Agent对话流程" -ForegroundColor Yellow
    
    $chatRequest = @{
        message = "帮我生成一个Hello World函数"
        conversationId = "test-integration-001"
    }
    
    try {
        $response = Invoke-RestMethod -Uri "$BaseUrl/api/agent/chat" `
            -Method POST `
            -ContentType "application/json" `
            -Body ($chatRequest | ConvertTo-Json -Depth 10)
        
        if ($response.code -eq 200 -and $response.data.message) {
            Write-Host "  ✅ Agent对话成功" -ForegroundColor Green
            return $true
        } else {
            Write-Host "  ❌ Agent对话失败" -ForegroundColor Red
            return $false
        }
    } catch {
        Write-Host "  ❌ 错误: $($_.Exception.Message)" -ForegroundColor Red
        return $false
    }
}

# 测试场景4: 诊断流程
function Test-DiagnosticFlow {
    Write-Host "🔍 测试场景4: 诊断流程" -ForegroundColor Yellow
    
    $diagnosticRequest = @{
        filePath = "test.cpp"
        code = "int main() { return 0; }"
        projectRoot = "C:/test"
    }
    
    try {
        $response = Invoke-RestMethod -Uri "$BaseUrl/api/completion/diagnostic/check" `
            -Method POST `
            -ContentType "application/json" `
            -Body ($diagnosticRequest | ConvertTo-Json -Depth 10)
        
        if ($response.code -eq 200) {
            Write-Host "  ✅ 诊断检查成功，发现 $($response.data.Count) 个问题" -ForegroundColor Green
            return $true
        } else {
            Write-Host "  ❌ 诊断检查失败" -ForegroundColor Red
            return $false
        }
    } catch {
        Write-Host "  ❌ 错误: $($_.Exception.Message)" -ForegroundColor Red
        return $false
    }
}

# 执行所有测试
$results = @()

Write-Host ""
$results += Test-CodeCompletionFlow
Write-Host ""
$results += Test-NavigationFlow
Write-Host ""
$results += Test-AgentFlow
Write-Host ""
$results += Test-DiagnosticFlow

Write-Host ""
Write-Host "📊 集成测试总结" -ForegroundColor Cyan
Write-Host "================================" -ForegroundColor Cyan
$passed = ($results | Where-Object { $_ -eq $true }).Count
$total = $results.Count
Write-Host "通过: $passed / $total" -ForegroundColor $(if ($passed -eq $total) { "Green" } else { "Yellow" })

exit $(if ($passed -eq $total) { 0 } else { 1 })

